﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NZBMatrixAdvancedFeedReader
{
    public partial class frmSearchTemplateDefinition : Form
    {

        public frmSearchTemplateDefinition()
        {
            InitializeComponent();
        }

        #region Properties

        public Classes.SearchTemplate SearchTemplate { get; set; }

        private Classes.SearchTemplate.SearchMethods SearchMethod
        {
            get
            {
                if (rbNZBName.Checked)
                    return Classes.SearchTemplate.SearchMethods.NZBName;
                else if (rbUsenetPostSubject.Checked)
                    return Classes.SearchTemplate.SearchMethods.UsenetPostSubject;
                else if (rbWeblink.Checked)
                    return Classes.SearchTemplate.SearchMethods.Weblink;
                return Classes.SearchTemplate.SearchMethods.NZBName;
            }
            set
            {
                switch (value)
                {
                    case NZBMatrixAdvancedFeedReader.Classes.SearchTemplate.SearchMethods.NZBName:
                        rbNZBName.Checked = true;
                        break;
                    case NZBMatrixAdvancedFeedReader.Classes.SearchTemplate.SearchMethods.UsenetPostSubject:
                        rbUsenetPostSubject.Checked = true;
                        break;
                    case NZBMatrixAdvancedFeedReader.Classes.SearchTemplate.SearchMethods.Weblink:
                        rbWeblink.Checked = true;
                        break;
                    default:
                        break;
                };
            }
        }

        private Classes.SearchTemplate.LinkTypes LinkType
        {
            get
            {
                if (rbNZBLinkDetailsLink.Checked)
                    return Classes.SearchTemplate.LinkTypes.NZBLinkDetailsLink;
                else if (rbNZBDownloadLink.Checked)
                    return Classes.SearchTemplate.LinkTypes.NZBDownloadLink;

                return Classes.SearchTemplate.LinkTypes.NZBLinkDetailsLink;
            }
            set
            {
                switch (value)
                {
                    case NZBMatrixAdvancedFeedReader.Classes.SearchTemplate.LinkTypes.NZBLinkDetailsLink:
                        rbNZBLinkDetailsLink.Checked = true;
                        break;
                    case NZBMatrixAdvancedFeedReader.Classes.SearchTemplate.LinkTypes.NZBDownloadLink:
                        rbNZBDownloadLink.Checked = true;
                        break;
                    default:
                        break;
                }
            }
        }

        public string Categories
        {

            get
            {

                StringBuilder sb;
                sb = new StringBuilder();

                foreach (string item in clbCategories.CheckedItems)
                {

                    if (sb.Length > 0)
                    {
                        sb.Append(",");
                    }

                    string key;
                    key = Classes.SearchTemplate.CategoriesLookup.FirstOrDefault(x => x.Value == item).Key;
                    sb.Append(key);

                }

                return sb.ToString();
            }

            set
            {
                foreach (string item in value.Split(','))
                {
                    string cat;
                    if (Classes.SearchTemplate.CategoriesLookup.TryGetValue(item, out cat))
                    {
                        int itemIndex;
                        itemIndex = clbCategories.FindString(cat);
                        clbCategories.SetItemChecked(itemIndex, true);
                    }
                }
            }

        }

        #endregion

        #region Event Handlers

        private void frmSearchTemplateDefinition_Load(object sender, EventArgs e)
        {

            //  Set the proper limit for Year Range
            nudMovieYearFrom.Maximum = DateTime.Now.Year;
            nudMovieYearTo.Maximum = DateTime.Now.Year;

            //  Load All Categories
            clbCategories.Items.Clear();
            foreach (var item in Classes.SearchTemplate.CategoriesLookup)
            {
                clbCategories.Items.Add(item.Value);
            }

            //  Load Data
            if (SearchTemplate != null)
            {
                txtName.Text = SearchTemplate.Name;
                txtSearchTerm.Text = SearchTemplate.SearchTerm;
                this.SearchMethod = SearchTemplate.SearchMethod;
                this.LinkType = SearchTemplate.LinkType;
                this.chkEnglishOnly.Checked = SearchTemplate.EnglishOnly;
                this.chkUseSSL.Checked = SearchTemplate.UseSSL;
                this.chkPreserveFilename.Checked = SearchTemplate.PreserveFileName;
                this.Categories = SearchTemplate.Categories;
                chkMovieYear.Checked = SearchTemplate.HasMovieYearLimit;
                if (chkMovieYear.Checked)
                {
                    nudMovieYearFrom.Value = decimal.Parse(SearchTemplate.MovieYearFrom);
                    nudMovieYearTo.Value = decimal.Parse(SearchTemplate.MovieYearTo);
                }

                chkMovieRate.Checked = SearchTemplate.HasMovieRateLimit;
                if (chkMovieRate.Checked)
                {
                    nudMovieRateFrom.Value = decimal.Parse(SearchTemplate.MovieRateFrom);
                    nudMovieRateTo.Value = decimal.Parse(SearchTemplate.MovieRateTo);
                }

                if (SearchTemplate.Name.ToUpper() == "Default".ToUpper())
                {
                    txtName.ReadOnly = true;
                    txtName.TabStop = false;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!IsValid())
            {
                return;
            }

            if (SearchTemplate == null)
            {
                SearchTemplate = new Classes.SearchTemplate();
            }
            SearchTemplate.Name = txtName.Text;
            SearchTemplate.SearchTerm = txtSearchTerm.Text;
            SearchTemplate.SearchMethod = this.SearchMethod;
            SearchTemplate.LinkType = this.LinkType;
            SearchTemplate.EnglishOnly = this.chkEnglishOnly.Checked;
            SearchTemplate.UseSSL = this.chkUseSSL.Checked;
            SearchTemplate.PreserveFileName = this.chkPreserveFilename.Checked;
            SearchTemplate.Categories = this.Categories;
            SearchTemplate.HasMovieYearLimit = chkMovieYear.Checked;
            SearchTemplate.MovieYearFrom = (chkMovieYear.Checked ? nudMovieYearFrom.Value.ToString() : "");
            SearchTemplate.MovieYearTo = (chkMovieYear.Checked ? nudMovieYearTo.Value.ToString() : "");

            SearchTemplate.HasMovieRateLimit = chkMovieRate.Checked;
            SearchTemplate.MovieRateFrom = (chkMovieRate.Checked ? nudMovieRateFrom.Value.ToString() : "");
            SearchTemplate.MovieRateTo = (chkMovieRate.Checked ? nudMovieRateTo.Value.ToString() : "");

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        #endregion

        #region Private Methods

        private bool IsValid()
        {
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                Classes.CommonTasks.ShowErrorMesssage("Please specify a name for this template.");
                return false;
            }

            //  Validate Duplicate Names
            Classes.SearchTemplate st = Classes.SystemSettings.SearchTemplateList.FirstOrDefault(x => x.Name.ToUpper() == txtName.Text.Trim().ToUpper());
            if (st != null && st != SearchTemplate)
            {
                Classes.CommonTasks.ShowErrorMesssage("The duplicate template exists in the system. Please use another name.");
                return false;
            }
            return true;
        }

        #endregion

    }
}
